<?php
//公共函数
use think\Db;
/**
 * 生成数据的返回值
 *
 * @param unknown $code
 * @param unknown $data
 */
function AjaxReturn($code,$msg,$data=array())
{

    $returnData=array(
        "code"=>$code
    );
    if($msg)
    {
        $returnData["message"]=$msg;
    }else
    {
        $returnData["message"]=getErrorInfo($code);
    }
    if(!empty($data))
    {
        $returnData["data"]=$data;
    }
    exit(json_encode($returnData,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT));
}
//获取code对应值
function getErrorInfo($code)
{
    $returnData=Db::name("system_code")->where("code",$code)->find();
    if(empty($returnData))
    {
        return "操作失败！";
    }else
    {
        return $returnData["message"];
    }
}
/**
 * 验证该请求是否是通过前台发出的
 * @param $timeStamp  时间戳
 * @param $randomStr  随机数
 * @param string            口令
 * @param $algorithm  算法规则
 */
function tokens($timeStamp,$randomStr,$string,$algorithm)
{
    $arr['timeStamp'] = $timeStamp;
    $arr['randomStr'] = $randomStr;
    $arr['string']          =  $string;

    //按照首字母大小写顺序排序
    sort($arr,SORT_STRING);
    //拼接成字符串
    $str = implode($arr);
    //进行加密
    $signature = sha1($str);
    $signature = md5($signature);
    //转换成大写
    $signature = strtoupper($signature);
    if ($signature == $algorithm)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}